extends Spatial

class_name DanceArea
var _startX=0
var _startY=0
var _endX=0
var _endY=0
var _gridDic={}
var _gridArr=[]

onready var _gridMap:GridMap=$GridMap
func _ready():
	G.danceArea=self
	var cells=_gridMap.get_used_cells()
	for cell in cells:
		var x=cell.x #x y 是格子左上角坐标
		var y=cell.z
		_startX=min(_startX,x)
		_startY=min(_startY,y)
		_endX=max(_endX,x)
		_endY=max(_endY,y)
		_setGrid(x,y)
		_gridArr.append(Vector2(x,y))
func _setGrid(x,y):
	if !_gridDic.has(x):
		_gridDic[x]={}
	_gridDic[x][y]=true
func hasGrid(x,y):
	if !_gridDic.has(x):
		return false
	if !_gridDic[x].has(y):
		return false
	return true
func getRandomPos()->Vector3:
	var index=int(rand_range(0,_gridArr.size()))
	var pos=_gridArr[index]
	var res=Vector3(pos.x+rand_range(0,1),0,pos.y+rand_range(0,1))
	return res
func posToGrid(pos:Vector3)->Vector2:
	return Vector2(
		int(pos.x),
		int(pos.z)
	)
func gridRandomPos(grid:Vector2)->Vector3:
	return Vector3(
		grid.x+rand_range(0,1),
		0,
		grid.y+rand_range(0,1)
	)
